///////////////////////////////////////////////////////////////////////////////////
// 基础方法调用接口封装
//开发人：虞胜强
//开发时间：2019/3/7
///////////////////////////////////////////////////////////////////////////////////
layui.define(['jquery','dialog','validUtil','layedit','layer'],function(exports){
    var $ = layui.jquery;
    var dialog = layui.dialog;
    var validUtil = layui.validUtil;
    var layedit = layui.layedit,
        layer = layui.layer;
    var baseutil = {
        /**
         * Post请求，成功返回结果
         *
         * @param url 地址
         * @param params 参数
         * @param successFn 操作成功，回调函数
         * @return
         * @author shengQiang yu
         * @date 2019-03-07 14:14
         */
        AjaxPostMark: function (url, params, successFn) {
            var index = layer.load(2);
            $.post(url, params, function (result) {
                layer.close(index);
                if (result.code === 0) {
                    //操作成功
                    successFn(result.data);
                } else {
                    //操作失败
                    dialog.msg(result.msg);
                }
            }, "json");
        },
        /**
         * Get请求，成功返回结果
         *
         * @param url 地址
         * @param params 参数
         * @param successFn 操作成功，回调函数
         * @return
         * @author shengQiang yu
         * @date 2019-03-07 14:14
         */
        AjaxGetMark: function (url, params, successFn) {
            var index = layer.load(2);
            $.get(url, params, function (result) {
                layer.close(index);
                if (result.code === 0) {
                    //操作成功
                    successFn(result.data);
                } else {
                    //操作失败
                    dialog.msg(result.msg);
                }
            }, "json");
        },
        post: function (url, params, successFn) {
            $.post(url, params, function (result) {
                successFn(result);
            }, "json");
        },
        get: function (url, params, successFn) {
            $.get(url, params, function (result) {
                successFn(result);
            });
        },
        /**
         * 获取文件类型，1、图片，2、pdf，3、其他
         * @param type 文件后缀
         * @author shengQiang yu
         * @date 2019-03-11 9:28
         */
        acceptFileType: function (type) {
            if (type=="jpg"||type=="png"||type=="gif"||type=="JPEG"){
               return "1";
            }else if(type=="pdf"){
                return "2";
            }else{
                return "3";
            }
        }
        /**
         * 获取随机数
         * @param num 随机个数
         * @author shengQiang yu
         * @date 2019-03-11 9:28
         */
        , acceptRandom: function (num) {
            var rnd = "";
            for (var i = 0; i < num; i++)
                rnd += Math.floor(Math.random() * 10);
            return rnd;
        }
        /**
         * 简单文本编辑器
         *
         * @param objId 对象Id
         * @param height 高度
         * @author shengQiang yu
         * @date 2019-03-11 9:28
         */
        , layeditBuild: function (objId, height) {
            if (height == null || height == "") {
                height = 400;
            }
            layedit.set({
                uploadImage: {
                    url: '/sysFile/layEditUpload' //接口url
                    , data: {catalog: "/sys/layedit"} //可选项。额外的参数，如：{id: 123, abc: 'xxx'}
                    , type: 'post' //默认post
                }
            });
            var index = layedit.build(objId, {
                tool: ['strong' //加粗
                    , 'italic' //斜体
                    , 'underline' //下划线
                    , 'del' //删除线
                    , '|' //分割线
                    , 'left' //左对齐
                    , 'center' //居中对齐
                    , 'right' //右对齐
                    , 'link' //超链接
                    , 'unlink' //清除链接
                    , 'image' //插入图片
                ],
                height: height+"px"
            });
            return index;
        },
        /**
         * 获取简单文本编辑器内容
         *
         * @param index 编辑器对象
         * @return
         * @author shengQiang yu
         * @date 2019-03-11 9:30
         */
        layeditBuildContent: function (index) {
            var xssCode="cyt_fwb_2019";//xss有效码格式cyt_fwb_4为数
            var content=layedit.getContent(index);
            if(!(isBlank(content))&&content.indexOf("cyt_fwb")>0){
                content=content.substring(24,content.length-4);
            }
            return "<p class='"+xssCode+"'>"+content+"</p>";
        }/**
         * KnifeZ文本编辑器
         *
         * @param objId 对象Id
         * @param height 高度
         * @author shengQiang yu
         * @date 2019-03-11 9:28
         */
        ,layeditKnifezBuild: function (objId, height) {
            if (height == null || height == "") {
                height = 400;
            }
            layedit.set({
                uploadImage: {
                    url: '/sysFile/layEditUpload',
                    data: {catalog: "/sys/layedit"}, //可选项。额外的参数，如：{id: 123, abc: 'xxx'}
                    type: 'post', //默认post
                    accept: 'image',
                    acceptMime: 'image/*',
                    exts: 'jpg|png|gif|bmp|jpeg',
                    size: 1024 * 10,
                    done: function (data) {
                        console.log(data);
                    }
                }
                , uploadVideo: {
                    url: '/sysFile/layEditUpload',
                    data: {catalog: "/sys/layedit"}, //可选项。额外的参数，如：{id: 123, abc: 'xxx'}
                    type: 'post', //默认post
                    accept: 'video',
                    acceptMime: 'video/*',
                    exts: 'mp4|flv|avi|rm|rmvb',
                    size: 1024 * 10 * 2,
                    done: function (data) {
                        console.log(data);
                    }
                }
                , uploadFiles: {
                    url: '/sysFile/layEditUpload',
                    data: {catalog: "/sys/layedit"}, //可选项。额外的参数，如：{id: 123, abc: 'xxx'}
                    type: 'post', //默认post
                    accept: 'file',
                    acceptMime: 'file/*',
                    size: '20480',
                    done: function (data) {
                        console.log(data);
                    }
                }
                , devmode: true
                , codeConfig: {
                    hide: false,  //是否显示编码语言选择框
                    default: 'javascript' //hide为true时的默认语言格式
                }
                , videoAttr: ' preload="none" '
                , tool: [
                    'html', 'undo', 'redo', 'code', 'strong', 'italic', 'underline', 'del', 'addhr', '|', 'fontFomatt', 'fontfamily','fontSize', 'fontBackColor', 'colorpicker', 'face'
                    , '|', 'left', 'center', 'right', '|', 'link', 'unlink', 'images', 'image_alt', 'video','attachment', '|'
                    , 'table', 'fullScreen'
                ]
                , height: height+'px'
            });
            var index = layedit.build(objId);
            return index;
        }/**
         * 时间控件
         *
         * @param objId 对象Id
         * @param range 是否范围true：是，false:否
         * @param successFn：回调函数
         * @author shengQiang yu
         * @date 2019-03-11 9:28
         */
        ,'layerDateTime': function (objId,successFn,range) {
            this.layerDateBase(objId,"yyyy-MM-dd HH:mm:ss",range,successFn);
        }
        /**
         * 日期控件
         *
         * @param objId 对象Id
         * @param range 是否范围true：是，false:否
         * @param successFn：回调函数
         * @author shengQiang yu
         * @date 2019-03-11 9:28
         */
        ,'layerDate': function (objId,successFn,range) {
            this.layerDateBase(objId,"yyyy-MM-dd",range,successFn);
        }
        /**
         * 日期时间基础控件
         *
         * @param objId 对象Id
         * @param format 日期格式
         * @param range 是否范围true：是，false:否
         * @param successFn：回调函数
         * @author shengQiang yu
         * @date 2019-03-11 9:28
         */
        ,'layerDateBase': function (objId, format, range,successFn) {
            if (format == "" || format == null) {
                format = "yyyy-MM-dd";
            }
            if (range == true) {
                range = true;
            } else {
                range = false;
            }
            layui.use('laydate', function () {
                var laydate = layui.laydate;
                laydate.render({
                    elem: '#' + objId //指定元素
                    , format: format
                    , range: range
                    , done: function (value, date) {
                        //通过回调函数设置值
                        if (typeof successFn == 'function') {
                            successFn(objId, value, date);
                        }
                    }
                });
            });
        }
    };
    //输出baseutil接口
    exports('baseutil', baseutil);
});

function isBlank(value) {
    return (value === null || "" === value || "undefined" === value || undefined === value);
}

function isInteger(value) {
    var reg = /^[1-9]\d*|0$/;
    return reg.test(value);
}

function isPositiveInteger(value) {
    var reg = /^\+?[1-9][0-9]*$/;
    return reg.test(value);
}

function isFloat(value) {
    var reg = /^[-\+]?\d+(\.\d+)?$/;
    return reg.test(value);
}

function moneyToCap(n) {
    var fraction = ['角', '分'];
    var digit = [
        '零', '壹', '贰', '叁', '肆',
        '伍', '陆', '柒', '捌', '玖'
    ];
    var unit = [
        ['元', '万', '亿'],
        ['', '拾', '佰', '仟']
    ];
    var head = n < 0 ? '欠' : '';
    n = Math.abs(n);
    var s = '';
    for (var i = 0; i < fraction.length; i++) {
        s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
    }
    s = s || '整';
    n = Math.floor(n);
    for (var i = 0; i < unit[0].length && n > 0; i++) {
        var p = '';
        for (var j = 0; j < unit[1].length && n > 0; j++) {
            p = digit[n % 10] + unit[1][j] + p;
            n = Math.floor(n / 10);
        }
        s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
    }
    return head + s.replace(/(零.)*零元/, '元')
        .replace(/(零.)+/g, '零')
        .replace(/^整$/, '零元整');
}